package com.osard.screen.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.osard.screen.enumerate.ScreenRecordDirectionEnum;
import com.osard.screen.event.ScreenRecordUserCancelEvent;
import com.osard.screen.service.ScreenRecordService;
import com.osard.screen.utils.ScreenRecordUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

/**
 * 用途：
 * <p>
 * 作者：MJSoftKing
 */
public class ScreenRecordActivity extends Activity {

    public static final int REQUEST_MEDIA_PROJECTION = 88888888;
    public static final String REQUESTED_ORIENTATION = "REQUESTED_ORIENTATION";
    public static final String TARGET_SERVICE = "TARGET_SERVICE";

    private Class<?> serviceName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //标题栏透明
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.TRANSPARENT);
        window.setNavigationBarColor(Color.TRANSPARENT);

        //参数
        Intent dataIntent = getIntent();

        //根据传入参数决定屏幕旋转方向
        Serializable requestedOrientation = dataIntent.getSerializableExtra(REQUESTED_ORIENTATION);
        if (requestedOrientation instanceof ScreenRecordDirectionEnum) {
            setRequestedOrientation(ScreenRecordDirectionEnum.PORTRAIT.equals(requestedOrientation) ?
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        serviceName = (Class<?>)dataIntent.getSerializableExtra(TARGET_SERVICE);

        //获取录屏服务
        MediaProjectionManager mMediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        //唤起录屏确认框
        startActivityForResult(
                mMediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_MEDIA_PROJECTION == requestCode && resultCode == Activity.RESULT_OK) {
            Bundle bundle = getIntent().getExtras();
            //启动录制服务
            Intent intent = new Intent(this, serviceName);
            //5个参数
            intent.putExtras(bundle);
            //2个屏幕截取参数
            intent.putExtra(ScreenRecordService.RESULT_CODE, resultCode);
            intent.putExtra(ScreenRecordService.RESULT_DATA, data);
            //启动服务
            startService(intent);
        } else {
            //用户取消
            EventBus.getDefault().post(new ScreenRecordUserCancelEvent());
        }
        this.finish();
    }

}
